//
//  ZallImageTextCell.m
//  ZJS-DRIVER
//
//  Created by so on 16/6/3.
//  Copyright © 2016年 liulei. All rights reserved.
//

#import "ZallImageTextCell.h"

@implementation ZallImageTextCell
@synthesize imageSize = _imageSize;
@synthesize imageView = _imageView;
@synthesize textLabel = _textLabel;

#pragma mark - life cycle
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        _imageSize = CGSizeZero;
        _autoFitImageSize = YES;
        _imageView = nil;
        _textLabel = nil;
    }
    return (self);
}
#pragma mark -

#pragma mark - override
- (void)layoutSubviews {
    [super layoutSubviews];
    CGRect insetFrame = UIEdgeInsetsInsetRect(self.contentView.bounds, _contentInsets);
    if(_imageView) {
        if(_autoFitImageSize) {
            _imageSize = CGSizeMake(CGRectGetHeight(insetFrame), CGRectGetHeight(insetFrame));
        }
        self.imageView.frame = CGRectMake(_contentInsets.left, _contentInsets.top, _imageSize.width, _imageSize.height);
        insetFrame.origin = CGPointMake(self.imageView.right + _gap.width, self.imageView.top);
        insetFrame.size = CGSizeMake(CGRectGetWidth(insetFrame) - self.imageView.width - _gap.width, CGRectGetHeight(insetFrame));
        if([self isAutoCenterY]) {
            self.imageView.centerY = CGRectGetMidY(insetFrame);
        }
    }
    if(_textLabel) {
        self.textLabel.frame = insetFrame;
    }
}

- (void)prepareForReuse {
    [super prepareForReuse];
    _imageSize = CGSizeZero;
    _autoFitImageSize = YES;
    if(_imageView) {
        _imageView.image = nil;
    }
    if(_textLabel) {
        _textLabel.text = nil;
    }
}
#pragma mark -

#pragma mark - setter
- (void)setImageSize:(CGSize)imageSize {
    
    [self willChangeValueForKey:@"imageSize"];
    _imageSize = imageSize;
    [self didChangeValueForKey:@"imageSize"];
    
    [self willChangeValueForKey:@"autoFitImageSize"];
    _autoFitImageSize = NO;
    [self didChangeValueForKey:@"autoFitImageSize"];
    
    [self setNeedsLayout];
}
#pragma mark -

#pragma mark - getter
- (UIImageView *)imageView {
    if(!_imageView) {
        _imageView = [[UIImageView alloc] init];
        [self.contentView addSubview:_imageView];
        [self setNeedsLayout];
    }
    return (_imageView);
}

- (UILabel *)textLabel {
    if(!_textLabel) {
        _textLabel = [[UILabel alloc] init];
        _textLabel.backgroundColor = [UIColor clearColor];
        _textLabel.font = [UIFont systemFontOfSize:16];
        _textLabel.numberOfLines = 0;
        [self.contentView addSubview:_textLabel];
        [self setNeedsLayout];
    }
    return (_textLabel);
}
#pragma mark -

@end
